<?php 
//all the class/function includes are managed in the includer file
include("include/includer.php");
//get all the configuration variables (custom function in newzbook.php include)
getConfigVars();  

//page specific variables
$playlistDirectory = "/share/Apps/NewzBook/playlists";//removemelater

//try to set the memory limit based on the configuration setting
if(ini_set("memory_limit",$config_memory_limit)!=TRUE){ 
    echo "Could not set memory to ".$config_memory_limit."B<br />\n"; 
}

//Get all the URL parameters (_GET)
if(isset($_GET['action'])) {
  $action = $_GET['action'];
}
if(isset($_GET['mode'])){
    $mode = $_GET['mode'];
}

//setup the file extensions before running the loop
$audioext = array("mp3","wma","aac","aif","flac","iff","m4a","m4b","m4r","mid","midi","mod","mpa","ogg","ptf","ra","ram","rmj","sib","snd","wav");
$videoext = array("mkv","wmv","avi","3g2","3gp","asf","asx","divx","dvr-ms","f4v","fbr","flv","m4v","mov","mp4","mpeg","mpg","mts","ogm","qt","rcproject","rm","rmvb","smil","srt","stx","swf","ts","vob","vro","xvid");
$photoext = array("xcf","tif","thm","tga","tex","svgz","svg","stl","shp","sdb","qxp","qxd","psp","psd","ps","ppm","png","pln","pdf","pct","obj","mng","mb","max","ma","jpx","jpg","jpeg","inx","indd","hdp","gif","fxg","fpf","exif","eps","emz","emf","dxf","dwg","drw","dng","dgn","cr2","cpt","cdr","br5","bmp","blend","art","ai","abm","3dmf","3dm");
$basicext = array("nfo","txt","wps","wpd","sig","rtf","pages","opml","msg","lst","log","lit","docx","doc");
$configext = array("themepack","reg","rdf","psf","prx","properties","prf","ini","icc","cfg","conf");

function endKey($array){
   end($array);
   return key($array);
}
function getJSONplaylist(){
    $notLast = true;
    //loop through the php session playlist and output it in the JSON format
    foreach($_SESSION['playlist'] as $key => $item){
        preg_match("/([^\`]+)\/([^\/]+)\.([^\/]+)$/",$item,$matches);
        //get the filename
        $filename = $matches[2].".".$matches[3];
        //get the directory
        $directory = $matches[1];
        //get the name from the file (without the extension)
        $name = $matches[2];
        //setup the URL to stream the file
        $fileURL = "fileman.php?path=".urlencode($directory)."&file=".urlencode($filename)."&action=stream";
        //output the format the jplayer likes
        echo "{name:\"$name\",mp3:\"$fileURL\"}";
        //if this is not the last file, add a comma to the end
        if(endKey($_SESSION['playlist']) == $key){ $notLast = false; }
        if($notLast){ echo ",\r\n"; }
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<HEAD>
    <?php standardHead("MediaPlayer");
    if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ ?>
    <link type="text/css" href="style/smoothness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />	
		<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
		<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
		<script type="text/javascript" src="js/fg.menu.js"></script>
    <link type="text/css" href="js/fg.menu.css" media="screen" rel="stylesheet" />
		<script type="text/javascript" src="js/jquery.jplayer.js"></script>
    <!--
      <script type="text/javascript" src="js/mediaplayer.js"></script>
    -->
    
    <script>
        <?php include("js/mediaplayer.js"); //this is not a properly formatted JS file. I need to work on cleaning this up to make it proper. ?>
    </script>
    
    <?php } ?>
    <link rel="stylesheet" type="text/css" href="style/mediaplayer/mediaplayer.css" />
    <!--[if IE 6]><link rel="stylesheet" type="text/css" href="style/mediaplayer/ie6.css" /><![endif]-->
    <!--[if IE 7]><link rel="stylesheet" type="text/css" href="style/mediaplayer/ie7.css" /><![endif]-->
    <!--[if IE]><link rel="stylesheet" type="text/css" href="style/mediaplayer/ie.css" /><![endif]-->
    
    <!-- Drop Down Menu Custom Styles-->
    <link rel="stylesheet" type="text/css" href="style/mediaplayer/dropmenu.css" />
    <!-- Drop Menu IE6 exceptions moved into the mediaplayer/ie6.css file -->

</HEAD>
<BODY>
<h2 class="headerlogo">NewzBook Media Player</h2>
<div id="playlistSelect">
    <?php if(isset($_SESSION['playlistLoaded'])){ 
              echo "<div class='small'><strong>Playlist:</strong> ".$_SESSION['playlistLoaded'];
              //if the loaded playlist hasn't been modified, show the last modified date of the file.
              if($_SESSION['playlistModified'] == false){
                  //check if the file exists
                  if(file_exists($playlistDirectory."/".$_SESSION['playlistLoaded'])){
                      echo "<br /><strong>Last Saved:</strong> ".date("M d Y g:i:s a", filemtime($playlistDirectory."/".$_SESSION['playlistLoaded']));
                  }
              }
              echo "</div>"; 
          } ?>
    <a tabindex="0" href="#action-menu" class="fg-button fg-button-icon-right ui-widget ui-state-default ui-corner-all" id="flat"><span class="ui-icon ui-icon-triangle-1-s"></span>File</a>
    <div id="search-engines" class="hidden">
    <ul>
      <li>
          <a href='playlistmanager.php?action=save' onClick="return popupSmall(this, 'action')" 
             title="Save Playlist"><img src='images/disk.png' alt="Save Playlist" /> Save Playlist</a>
      </li>
      <li>
          <a href='playlistmanager.php?action=new' onClick="return popupSmall(this, 'action')" 
             title="Save As"><img src='images/disk_multiple.png' alt="Save As" /> Save As</a>
      </li>
      <li>
          <a href='playlistmanager.php?action=open' onClick="return popupSmall(this, 'action')" 
             title="Open Playlist"><img src='images/folder_page.png' alt="Open playlist" /> Open Playlist</a>
      </li>
      <li>
          <a href='playlistmanager.php?action=delete' onClick="return popupSmall(this, 'action')" 
             title="Delete Playlist"><img src='images/delete.png' alt="Delete playlist" /> Delete / Clear Playlist</a>
      </li>
      <!-- <li>
                <span class="refreshPlaylist"><img src='images/arrow_refresh.png' alt='Refresh Playlist' /> Refresh Playlist</span>
           </li>
        -->
    <li><a href='#'>Other</a>
        <ul>
            <li>
                <a href='playlistmanager.php?action=displayRemoteController' onClick="return popupController(this, 'controller')" 
                   title="Controller"><img src='images/controller.png' /> Remote Controller</a>
            </li>
            <li>
                <a href='playlistmanager.php?action=remoteplay&mode=playlist' onClick="return popupSmall(this, 'action')" 
                   title="Playlist"><img src='images/server_go.png' /> Start Playlist on PCH</a>
            </li>
            <li>
            <!-- <div class='dbgLvl1' style='display:none;visibility:hidden;'> -->
              <li>
                  <a href='playlistmanager.php?action=getJSONplaylist' onClick="return popupSmall(this, 'action')" 
                     title="Get JSON Playlist"><img src='images/bug.png' alt="Get JSON Playlist" /> Get JSON Playlist</a>
              </li>
              <li>
                  <a href='playlistmanager.php?action=debug' onClick="return popupSmall(this, 'action')" 
                     title="Debug Playlist"><img src='images/bug.png' alt="Debug Playlist" /> Debug Playlist</a>
              </li>
            <!-- </div> -->
        </ul>
    </li>
    </ul>
    </div>
    <img src='images/arrow_refresh.png' id="refreshPlaylist" />
</div>
<div id="jquery_jplayer"></div>

<div id="player_container">
	<ul id="icons" class="ui-widget ui-helper-clearfix">
		<li id="ctrl_prev" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-seek-first"></span></li>
		
		<li id="play" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-play"></span></li>
		<li id="pause" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-pause"></span></li>
    
		<li id="stop" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-stop"></span></li>

    <li id="ctrl_next" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-seek-end"></span></li>
    
		<li id="volume-min" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-volume-off"></span></li>
		<li id="volume-max" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-volume-on"></span></li>
	</ul>

	<!-- Sliders -->
	<div id="sliderVolume"></div>

	<div id="bars_holder">
		<div id="sliderPlayback"></div>
		<div id="loaderBar"></div>
	</div>
</div>

<div id="playlist_list">
	<ul>
		<!-- The function displayPlayList() uses this unordered list -->
	</ul>
</div>
</BODY>